package com.jzlin.funhub.admin.service.file.strategy;

import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.StrUtil;
import com.jzlin.funhub.admin.common.enums.FileStorageStrategyEnum;
import com.jzlin.funhub.admin.config.properties.FileStorageProperties;
import com.jzlin.funhub.framework.core.exception.PropertiesException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

/**
 * 文件存储策略-工厂bean.
 *
 * @author linjz
 * @since 2025/8/10
 */
@Component
public class FileStorageStrategyFactoryBean implements FactoryBean<IFileStorageStrategy> {
    FileStorageProperties properties;

    public FileStorageStrategyFactoryBean(FileStorageProperties properties) {
        this.properties = properties;
    }

    @Override
    public IFileStorageStrategy getObject() {
        if (StrUtil.isBlank(properties.getStrategy())) {
            throw new PropertiesException("文件存储策略不能为空！");
        }

        switch (FileStorageStrategyEnum.valueOf(properties.getStrategy())) {
            case LOCAL:
                return buildLocalFileStoreStrategy(properties);
        }
        return null;
    }

    private LocalFileStoreStrategy buildLocalFileStoreStrategy(FileStorageProperties properties) {
        return new LocalFileStoreStrategy(properties);
    }

    @Override
    public Class<?> getObjectType() {
        return IFileStorageStrategy.class;
    }
}
